home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / LineNumberInputStream.java < prev    next >
Text File  |  1998-09-22  |  9KB  |  260 lines

  1. /*
  2.  * @(#)LineNumberInputStream.java    1.13 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * This class is an input stream filter that provides the added 
  19.  * functionality of keeping track of the current line number. 
  20.  * <p>
  21.  * A line is a sequence of bytes ending with a carriage return 
  22.  * character (<code>'\r'</code>), a newline character 
  23.  * (<code>'\n'</code>), or a carriage return character followed 
  24.  * immediately by a linefeed character. In all three cases, the line 
  25.  * terminating character(s) are returned as a single newline character.
  26.  * <p>
  27.  * The line number begins at <code>0</code>, and is incremented by 
  28.  * <code>1</code> when a <code>read</code> returns a newline character.
  29.  *
  30.  * @author     Arthur van Hoff
  31.  * @version    1.13, 07/01/98
  32.  * @see        java.io.LineNumberReader
  33.  * @since      JDK1.0
  34.  * @deprecated This class incorrectly assumes that bytes adequately represent
  35.  *             characters.  As of JDK 1.1, the preferred way to operate on
  36.  *             character streams is via the new character-stream classes, which
  37.  *             include a class for counting line numbers.
  38.  */
  39. public
  40. class LineNumberInputStream extends FilterInputStream {
  41.     int pushBack = -1;
  42.     int lineNumber;
  43.     int markLineNumber;
  44.  
  45.     /**
  46.      * Constructs a newline number input stream that reads its input 
  47.      * from the specified input stream. 
  48.      *
  49.      * @param      in   the underlying input stream.
  50.      * @since      JDK1.0
  51.      */
  52.     public LineNumberInputStream(InputStream in) {
  53.     super(in);
  54.     }
  55.  
  56.     /**
  57.      * Reads the next byte of data from this input stream. The value 
  58.      * byte is returned as an <code>int</code> in the range 
  59.      * <code>0</code> to <code>255</code>. If no byte is available 
  60.      * because the end of the stream has been reached, the value 
  61.      * <code>-1</code> is returned. This method blocks until input data 
  62.      * is available, the end of the stream is detected, or an exception 
  63.      * is thrown. 
  64.      * <p>
  65.      * The <code>read</code> method of 
  66.      * <code>LineNumberInputStream</code> calls the <code>read</code> 
  67.      * method of the underlying input stream. It checks for carriage 
  68.      * returns and newline characters in the input, and modifies the 
  69.      * current line number as appropriate. A carriage-return character or 
  70.      * a carriage return followed by a newline character are both 
  71.      * converted into a single newline character. 
  72.      *
  73.      * @return     the next byte of data, or <code>-1</code> if the end of this
  74.      *             stream is reached.
  75.      * @exception  IOException  if an I/O error occurs.
  76.      * @see        java.io.FilterInputStream#in
  77.      * @see        java.io.LineNumberInputStream#getLineNumber()
  78.      * @since      JDK1.0
  79.      */
  80.     public int read() throws IOException {
  81.     int c = pushBack;
  82.  
  83.     if (c != -1) {
  84.         pushBack = -1;
  85.     } else {
  86.         c = in.read();
  87.     }
  88.  
  89.     switch (c) {
  90.       case '\r':
  91.         pushBack = in.read();
  92.         if (pushBack == '\n') {
  93.         pushBack = -1;
  94.         }
  95.       case '\n':
  96.         lineNumber++;
  97.         return '\n';
  98.     }
  99.     return c;
  100.     }
  101.  
  102.     /**
  103.      * Reads up to <code>len</code> bytes of data from this input stream 
  104.      * into an array of bytes. This method blocks until some input is available.
  105.      * <p>
  106.      * The <code>read</code> method of 
  107.      * <code>LineNumberInputStream</code> repeatedly calls the 
  108.      * <code>read</code> method of zero arguments to fill in the byte array.
  109.      *
  110.      * @param      b     the buffer into which the data is read.
  111.      * @param      off   the start offset of the data.
  112.      * @param      len   the maximum number of bytes read.
  113.      * @return     the total number of bytes read into the buffer, or
  114.      *             <code>-1</code> if there is no more data because the end of
  115.      *             this stream has been reached.
  116.      * @exception  IOException  if an I/O error occurs.
  117.      * @see        java.io.LineNumberInputStream#read()
  118.      * @since      JDK1.0
  119.      */
  120.     public int read(byte b[], int off, int len) throws IOException {
  121.     if (len <= 0) {
  122.         return 0;
  123.     }
  124.  
  125.     int c = read();
  126.     if (c == -1) {
  127.         return -1;
  128.     }
  129.     b[off] = (byte)c;
  130.  
  131.     int i = 1;
  132.     try {
  133.         for (; i < len ; i++) {
  134.         c = read();
  135.         if (c == -1) {
  136.             break;
  137.         }
  138.         if (b != null) {
  139.             b[off + i] = (byte)c;
  140.         }
  141.         }
  142.     } catch (IOException ee) {
  143.     }
  144.     return i;
  145.     }
  146.  
  147.     /**
  148.      * Sets the line number to the specified argument. 
  149.      *
  150.      * @param      lineNumber   the new line number.
  151.      * @since      JDK1.0
  152.      */
  153.     public void setLineNumber(int lineNumber) {
  154.     this.lineNumber = lineNumber;
  155.     }
  156.  
  157.     /**
  158.      * Returns the current line number.
  159.      *
  160.      * @return     the current line number.
  161.      * @since      JDK1.0
  162.      */
  163.     public int getLineNumber() {
  164.     return lineNumber;
  165.     }
  166.  
  167.     /**
  168.      * Skips over and discards <code>n</code> bytes of data from the 
  169.      * input stream. The <code>skip</code> method may, for a variety of 
  170.      * reasons, end up skipping over some smaller number of bytes, 
  171.      * possibly <code>0</code>. The actual number of bytes skipped is returned.
  172.      * <p>
  173.      * The <code>skip</code> method of 
  174.      * <code>LineNumberInputStream</code> creates a byte array of length 
  175.      * <code>n</code> and then reads into it until <code>n</code> bytes 
  176.      * have been read or the end of the stream has been reached. 
  177.      *
  178.      * @param      n   the number of bytes to be skipped.
  179.      * @return     the actual number of bytes skipped.
  180.      * @exception  IOException  if an I/O error occurs.
  181.      * @since      JDK1.0
  182.      */
  183.     public long skip(long n) throws IOException {
  184.     return read(new byte[(int)n]);
  185.     }
  186.  
  187.     /**
  188.      * Returns the number of bytes that can be read from this input 
  189.      * stream without blocking. 
  190.      * <p>
  191.      * Note that if the underlying input stream is able to supply 
  192.      * <i>k</i> input characters without blocking, the 
  193.      * <code>LineNumberInputStream</code> can guarantee only to provide 
  194.      * <i>k</i>/2 characters without blocking, because the 
  195.      * <i>k</i> characters from the underlyhing input stream might 
  196.      * consist of <i>k</i>/2 pairs of <code>'\r'</code> and 
  197.      * <code>'\n'</code>, which are converted to just 
  198.      * <i>k</i>/2 <code>'\n'</code> characters. 
  199.      *
  200.      * @return     the number of bytes that can be read from this input stream
  201.      *             without blocking.
  202.      * @exception  IOException  if an I/O error occurs.
  203.      * @see        java.io.FilterInputStream#in
  204.      * @since      JDK1.0
  205.      */
  206.     public int available() throws IOException {
  207.     return (pushBack == -1) ? super.available() : super.available() + 1;
  208.     }
  209.  
  210.     /**
  211.      * Marks the current position in this input stream. A subsequent 
  212.      * call to the <code>reset</code> method repositions this stream at 
  213.      * the last marked position so that subsequent reads re-read the same bytes.
  214.      * <p>
  215.      * The <code>mark</code> method of 
  216.      * <code>LineNumberInputStream</code> remembers the current line 
  217.      * number in a private variable, and then calls the <code>mark</code> 
  218.      * method of the underlying input stream. 
  219.      *
  220.      * @param   readlimit   the maximum limit of bytes that can be read before
  221.      *                      the mark position becomes invalid.
  222.      * @see     java.io.FilterInputStream#in
  223.      * @see     java.io.LineNumberInputStream#reset()
  224.      * @since   JDK1.0
  225.      */
  226.     public void mark(int readlimit) {
  227.     markLineNumber = lineNumber;
  228.     in.mark(readlimit);
  229.     }
  230.  
  231.     /**
  232.      * Repositions this stream to the position at the time the 
  233.      * <code>mark</code> method was last called on this input stream. 
  234.      * <p>
  235.      * The <code>reset</code> method of 
  236.      * <code>LineNumberInputStream</code> resets the line number to be 
  237.      * the line number at the time the <code>mark</code> method was 
  238.      * called, and then calls the <code>reset</code> method of the 
  239.      * underlying input stream. 
  240.      * <p>
  241.      * Stream marks are intended to be used in
  242.      * situations where you need to read ahead a little to see what's in
  243.      * the stream. Often this is most easily done by invoking some
  244.      * general parser. If the stream is of the type handled by the
  245.      * parser, it just chugs along happily. If the stream is not of
  246.      * that type, the parser should toss an exception when it fails,
  247.      * which, if it happens within readlimit bytes, allows the outer
  248.      * code to reset the stream and try another parser.
  249.      *
  250.      * @exception  IOException  if an I/O error occurs.
  251.      * @see        java.io.FilterInputStream#in
  252.      * @see        java.io.LineNumberInputStream#mark(int)
  253.      * @since      JDK1.0
  254.      */
  255.     public void reset() throws IOException {
  256.     lineNumber = markLineNumber;
  257.     in.reset();
  258.     }
  259. }
  260.